package com.ds.home.service.impl;

import com.ds.common.query.Condition;
import com.ds.common.query.JLuceneIndex;
import com.ds.config.ErrorListResultModel;
import com.ds.config.ErrorResultModel;
import com.ds.config.ListResultModel;
import com.ds.config.ResultModel;
import com.ds.context.JDSActionContext;
import com.ds.esb.config.EsbBeanAnnotation;
import com.ds.home.engine.AppEngine;
import com.ds.home.query.HomeConditionKey;
import com.ds.home.query.IOTConditionKey;
import com.ds.iot.Device;
import com.ds.iot.DeviceEndPoint;
import com.ds.iot.HomeException;
import com.ds.iot.ZNode;
import com.ds.iot.api.inner.DeviceSearchService;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@EsbBeanAnnotation(id = "DeviceSearchService", name = "DeviceSearchService", expressionArr = "DeviceSearchServiceImpl()", desc = "DeviceSearchService")

public class DeviceSearchServiceImpl implements DeviceSearchService {

    private final AppEngine appEngine;

    public DeviceSearchServiceImpl() {

        this.appEngine = AppEngine.getEngine(JDSActionContext.getActionContext().getSystemCode());
    }

    @Override
    public ListResultModel<List<ZNode>> loadZNodeList(String[] strings) {
        ListResultModel<List<ZNode>> result = new ListResultModel<List<ZNode>>();
        List<ZNode> znodes = new ArrayList<ZNode>();
        try {
            for (String znodeId : strings) {
                znodes.add(appEngine.getZNodeById(znodeId));
            }
            result.setData(znodes);
        } catch (HomeException e) {
            result = new ErrorListResultModel<>();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ListResultModel<List<Device>> loadDeviceList(String[] strings) {
        ListResultModel<List<Device>> result = new ListResultModel<List<Device>>();
        List<Device> devices = new ArrayList<Device>();
        try {
            for (String deviceid : strings) {
                devices.add(appEngine.getDeviceById(deviceid));
            }
            result.setData(devices);
        } catch (HomeException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ListResultModel<List<DeviceEndPoint>> loadDeviceEndPointList(String[] strings) {
        ListResultModel<List<DeviceEndPoint>> result = new ListResultModel<List<DeviceEndPoint>>();
        List<DeviceEndPoint> eps = new ArrayList<DeviceEndPoint>();
        try {
            for (String epid : strings) {
                eps.add(appEngine.getEndPoint(epid));
            }
            result.setData(eps);
        } catch (HomeException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }


    @Override
    public ResultModel<ZNode> findZNodeByIeee(String serialno, String userID) {
        ResultModel<ZNode> result = new ResultModel<ZNode>();
        List<String> ids = new ArrayList<String>();
        try {
            ZNode znode = appEngine.findZNodeByIeee(serialno, userID);
            result.setData(znode);
        } catch (HomeException e) {
            result = new ErrorResultModel();
            ((ErrorResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @Override
    public ListResultModel<Set<String>> findGWDevicesByFactory(String factoryName) {
        ListResultModel<Set<String>> result = new ListResultModel<Set<String>>();
        Set<String> deviceIds = appEngine.findGWDevicesByFactory(factoryName);
        result.setData(deviceIds);

        return result;
    }

    @Override
    public ListResultModel<Set<String>> findPlace(Condition<HomeConditionKey, JLuceneIndex> condition) {

        return appEngine.findPlace(condition);
    }

    @Override
    public ListResultModel<Set<String>> findArea(Condition<HomeConditionKey, JLuceneIndex> condition) {
        return appEngine.findArea(condition);
    }

    @Override
    public ListResultModel<Set<String>> findDevice(Condition<IOTConditionKey, JLuceneIndex> condition) {
        return appEngine.findDevice(condition);
    }

    @Override
    public ListResultModel<Set<String>> findEndPoint(Condition<IOTConditionKey, JLuceneIndex> condition) {
        return appEngine.findEndPoint(condition);
    }

    @Override
    public ListResultModel<Set<String>> findZNode(Condition<IOTConditionKey, JLuceneIndex> condition) {
        return appEngine.findZNode(condition);
    }

    @Override
    public ListResultModel<Set<String>> findAlarm(Condition<HomeConditionKey, JLuceneIndex> condition) {
        return appEngine.findAlarm(condition);
    }

    @Override
    public ListResultModel<Set<String>> findScene(Condition<HomeConditionKey, JLuceneIndex> condition) {
        return appEngine.findScene(condition);
    }
}
